import router from './router'
import store from './store'
import $com from '@/utils/open/common'
import $eltool from '@/utils/open/eltool'
import originalRouter from './router/original'

// NProgress.configure({ showSpinner: false })

router.beforeEach((to, from, next) => {
  if(!store.state.isLoading){ // 未登录
    // 刷新处理
    if(location.hash!=='#/'){
      window.location.href='./'
    }

    store.commit('upDateLoadingStatus',true)

    // 1.固定路由(原始路由)
    store.commit('upDateRouters',originalRouter)

    // 2.动态添加路由
    let addList = [
      {
        path: '/',
        name: '',
        component: 'views/first',
        meta: {icon: '', title: '一级-first/list1'},
        children:[
          {path: 'List11', name: 'List11', meta: {icon: '', title: 'List11'}, component: 'views/list1/list1-1.vue'},
          {path: 'List12', name: 'List12', meta: {icon: '', title: 'List12'}, component: 'views/list1/list1-2.vue'},
          {path: 'List13', name: 'List13', meta: {icon: '', title: 'List13'}, component: 'views/list1/list1-3.vue'}
        ]
      },
      {
        path: '/',
        name: '',
        component: 'views/first',
        meta: {icon: '', title: '一级-first/list2'},
        children:[
          {path: 'List21', name: 'List21', meta: {icon: '', title: 'List21'}, component: 'views/list2/list2-1.vue'},
          // {path: 'List21', name: 'List21', component: (resolve) => require(['./views/list2/list2-1.vue'], resolve)},
          // {path: 'List22', name: 'List22', component: (resolve) => require(['./views/list2/list2-2.vue'], resolve)},
          // {path: 'List23', name: 'List23', component: (resolve) => require(['./views/list2/list2-3.vue'], resolve)}
        ]
      }
    ]

    let addRouterArr = $com.formatRoutes(addList) // 格式化路由
    store.commit('upDateAddRouteList',addRouterArr)

    // 3.总的路由
    let allRouter = originalRouter.concat(addRouterArr)
    store.commit('upDateAllRouters',allRouter)

    // console.log('originalRouter:',store.state.routers)
    // console.log('addRouterArr:',store.state.addRouteList)
    // console.log('allRouter:',store.state.routerAllList)
    router.addRoutes(store.state.routerAllList) // 动态添加可访问路由表

  }
  // console.log(to.path)
  next()
})